﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for SearchProcessor
/// </summary>
/// 
public class SearchProcessor
{
    public string search_criteria; 
           
    private string[] _parsed_string; 
    
    public string[] parsed_string
    { 
        get { return _parsed_string; }

        set { _parsed_string = value; } 
    }

    private string _condition;
    
    public string condition 
    { 
        get { return _condition; }

        set { _condition = value;} 
    }

    public SearchProcessor()
    {

    }
    
    public string[] parse_to_array(string search_string)
    {        
        string[] divided = search_string.Split(' ');

        List<string> lst = new List<string>();

        for (int i = 0; i < divided.Length; i++)
        {
            if (divided[i] != "")
            {
                lst.Add(divided[i]); 
            }
        }

        string[] parsed = new string[lst.Count];

        lst.CopyTo(parsed); 

        return parsed; 
    }

    public string create_condition()
    {
        string cond = "";

        process_exclamation();

        string[] prepared = parse_to_array(search_criteria);

        for (int i = 0; i < prepared.Length; i++)
        {
            
            if (i != prepared.Length - 1)
            {
                    cond = cond + prepared[i] + " OR ";
            }
            else
            {
                cond = cond + prepared[i];
            }
        }

        return cond;
    }

    public string[] check_for_city(string[] parsed_search)
    { 
        dbProcessor my_db=new dbProcessor();

        for (int i = 0; i < parsed_search.Length; i++)
        { 
           if(my_db.isCity(parsed_search[i].ToString()))
         
            {
                parsed_search[i] = parsed_search[i] + " - city";
            }
           else
           {
               parsed_search[i] = parsed_search[i] + " - not city";
           }
        }
        
        return parsed_search; 
    }

    private string process_exclamation()
    {
        if (search_criteria.Contains("!"))
        {
             search_criteria=search_criteria.Replace('!', ' '); 
        }
        return search_criteria;
    }

    public string highlight_letters(string s, string prefix)
    { 
        string result = "<B>"+s.Insert(prefix.Length,"</B>"); 
        return result; 
    }
}